#include "SelectManager.h"
#include "MeshActor.h"
#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkMinimalStandardRandomSequence.h>
#include <vtkNamedColors.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkAssembly.h>
#include <vtkPropAssembly.h>
#include "QRenderWindowStyle.h"
#include "Selection.h"
#include "SelectorHighlight.h"
#include <vtkAppendPolyData.h>
#include <vtkCompositePolyDataMapper.h>
#include <vtkUnstructuredGrid.h>
#include <vtkTriangle.h>
#include <vtkMultiBlockDataSet.h>

void SelectManager::bindRenderer(vtkRenderer* renderer)
{
	this->renderer_ = renderer;
}

void SelectManager::select(double posx, double posy)
{
	if (this->selector_) {
		assert(this->select_mode_ != SelectMode::None);
		this->selector_->select(posx, posy);
	}
}

void SelectManager::setSelectActor(const MeshActor* model_actor_)
{
	//assert(this->selector_);

	this->cur_model_actor_ = model_actor_;
	if (this->selector_)
	{
		this->selector_->clear();
		this->selector_->getPickList()->RemoveAllItems();
		if (model_actor_)
		{
			this->cur_model_actor_->addPickList(this->selector_->getPickList());
		}
	}
		
	
}

void SelectManager::setSelectMode(SelectMode select_mode)
{
	this->select_mode_=select_mode;
	if (this->select_mode_ == SelectMode::Face)
	{
		if (this->selector_)
		{
			this->selector_->clear();
		}
		this->selector_ = std::make_unique<SingleFaceSelectorHighlight>(this->renderer_);
		this->selector_->getPickList()->RemoveAllItems();
		if (this->cur_model_actor_)
		{
			this->cur_model_actor_->addPickList(this->selector_->getPickList());
		}
	}
	else if (this->select_mode_ == SelectMode::Block)
	{
		if (this->selector_)
		{
			this->selector_->clear();
		}
		this->selector_ = std::make_unique<BlockSelectorHighlight>(this->renderer_);
		this->selector_->getPickList()->RemoveAllItems();
		if (this->cur_model_actor_)
		{
			this->cur_model_actor_->addPickList(this->selector_->getPickList());
		}
	}
	else if (this->select_mode_ == SelectMode::Edge)
	{
		if (this->selector_)
		{
			this->selector_->clear();
		}		this->selector_ = std::make_unique<SingleEdgeSelectorHighlight>(this->renderer_);
		this->selector_->getPickList()->RemoveAllItems();
		if (this->cur_model_actor_)
		{
			this->cur_model_actor_->addPickList(this->selector_->getPickList());
		}
	}
	else
	{
		assert(this->select_mode_ == SelectMode::None);
		if (this->selector_) {
			this->selector_->clear();
		}
		this->selector_ = nullptr;
	}
}

void SelectManager::clearSelection()
{
	if (this->selector_) {
		this->selector_->clear();
	}
}

std::unique_ptr<Selection> SelectManager::getSelection()
{
	std::unique_ptr<Selection> selection = std::make_unique<Selection>();

	if (this->select_mode_ == SelectMode::Face) {
		for (const auto& id : this->selector_->get().ids) {
			selection->ids.push_back(id);
		}
		selection->type = ElementEnum::Face;
	}
	else if (this->select_mode_ == SelectMode::Block) {
		for (const auto& id : this->selector_->get().ids) {
			selection->ids.push_back(id);
		}
		selection->type = ElementEnum::Block;
	}
	else if (this->select_mode_ == SelectMode::Edge) {
		for(const auto& id : this->selector_->get().ids) {
			selection->ids.push_back(id);
		}
		selection->type = ElementEnum::Edge;
	}
	else {
		if (this->selector_)
			this->selector_->clear();
		return nullptr;
	}

	return selection;
}
